package com.eshop.modules.shop.service.impl;

import java.lang.invoke.SerializedLambda;
import java.io.IOException;
import java.util.Iterator;
import com.eshop.utils.FileUtil;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.eshop.common.utils.QueryHelpPlus;
import com.eshop.modules.shop.service.dto.SystemStoreDto;
import java.util.LinkedHashMap;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.shop.service.dto.SystemStoreQueryCriteria;
import com.eshop.utils.location.LocationUtils;
import com.eshop.utils.RedisUtil;
import com.eshop.utils.ShopKeyUtils;
import com.eshop.enums.ShopCommonEnum;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import java.io.Serializable;
import com.eshop.modules.product.vo.SystemStoreQueryVo;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.shop.service.SystemStoreService;
import com.eshop.modules.shop.domain.SystemStore;
import com.eshop.modules.shop.service.mapper.SystemStoreMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class SystemStoreServiceImpl extends BaseServiceImpl<SystemStoreMapper, SystemStore> implements SystemStoreService
{
    private final IGenerator generator;
    private final SystemStoreMapper systemStoreMapper;
    
    public SystemStoreQueryVo getYxSystemStoreById(final int id) {
        return (SystemStoreQueryVo)this.generator.convert(this.getById((Serializable)id), (Class)SystemStoreQueryVo.class);
    }
    
    public List<SystemStoreQueryVo> getStoreList(final String latitude, final String longitude, final int page, final int limit) {
        final Page<SystemStore> pageModel = (Page<SystemStore>)new Page((long)page, (long)limit);
        if (StrUtil.isBlank((CharSequence)latitude) || StrUtil.isBlank((CharSequence)longitude)) {
            return (List<SystemStoreQueryVo>)this.generator.convert(((Page)this.page((IPage)pageModel)).getRecords(), (Class)SystemStoreQueryVo.class);
        }
        final List<SystemStoreQueryVo> list = this.systemStoreMapper.getStoreList(pageModel, Double.valueOf(longitude), Double.valueOf(latitude));
        list.forEach(item -> {
            String newDis = NumberUtil.round(Double.valueOf(item.getDistance()) / 1000.0, 2).toString();
            item.setDistance(newDis);
            return;
        });
        return list;
    }
    
    public SystemStoreQueryVo getStoreInfo(final String latitude, final String longitude) {
        final SystemStore systemStore = this.systemStoreMapper.selectOne(((Wrappers.<SystemStore>lambdaQuery().eq(SystemStore::getIsShow, ShopCommonEnum.SHOW_1.getValue())).orderByDesc(SystemStore::getId)).last("limit 1"));
        if (systemStore == null) {
            return null;
        }
        final String mention = (String)RedisUtil.get(ShopKeyUtils.getStoreSelfMention());
        if (StrUtil.isBlank((CharSequence)mention) || ShopCommonEnum.ENABLE_2.getValue().toString().equals(mention)) {
            return null;
        }
        final SystemStoreQueryVo systemStoreQueryVo = (SystemStoreQueryVo)this.generator.convert(systemStore, (Class)SystemStoreQueryVo.class);
        if (StrUtil.isNotEmpty((CharSequence)latitude) && StrUtil.isNotEmpty((CharSequence)longitude)) {
            final double distance = LocationUtils.getDistance((double)Double.valueOf(latitude), (double)Double.valueOf(longitude), (double)Double.valueOf(systemStore.getLatitude()), (double)Double.valueOf(systemStore.getLongitude()));
            systemStoreQueryVo.setDistance(String.valueOf(distance));
        }
        return systemStoreQueryVo;
    }
    
    public Map<String, Object> queryAll(final SystemStoreQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<SystemStore> page = (PageInfo<SystemStore>)new PageInfo((List)this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", this.generator.convert(page.getList(), (Class)SystemStoreDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<SystemStore> queryAll(final SystemStoreQueryCriteria criteria) {
        return (List<SystemStore>)((SystemStoreMapper)this.baseMapper).selectList((Wrapper)QueryHelpPlus.getPredicate(SystemStore.class, criteria));
    }
    
    public void download(final List<SystemStoreDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final SystemStoreDto yxSystemStore : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("门店名称", yxSystemStore.getName());
            map.put("简介", yxSystemStore.getIntroduction());
            map.put("手机号码", yxSystemStore.getPhone());
            map.put("省市区", yxSystemStore.getAddress());
            map.put("详细地址", yxSystemStore.getDetailedAddress());
            map.put("门店logo", yxSystemStore.getImage());
            map.put("纬度", yxSystemStore.getLatitude());
            map.put("经度", yxSystemStore.getLongitude());
            map.put("核销有效日期", yxSystemStore.getValidTime());
            map.put("每日营业开关时间", yxSystemStore.getDayTime());
            map.put("是否显示", yxSystemStore.getIsShow());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public SystemStoreServiceImpl(final IGenerator generator, final SystemStoreMapper systemStoreMapper) {
        this.generator = generator;
        this.systemStoreMapper = systemStoreMapper;
    }
}
